package com.sdkx.app.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.AlarmPointManager;
import com.sdkx.app.activitys.MainActivity;
import com.sdkx.app.dialog.AlarmInfoDialog;
import com.sdkx.app.datalayers.AlarmAreaLayer;
import com.sdkx.app.datalayers.LayersFactory;
import com.sdkx.app.model.AlarmPoint;
import com.sdkx.app.view.BrokenLineView;

import java.util.ArrayList;
import java.util.List;

public class AlarmFragment extends BaseFragment implements View.OnClickListener {
    ListView mListView;
    EditText mSearchEdit;
    AlarmAdapter alarmAdapter;
    @Nullable
    @Override
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_menu_alarm1, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //返回上一级点击事件
        Button tv_all_air_spaces = (Button) view.findViewById(R.id.tv_all_air_spaces2);

        //返回上一级按钮
        tv_all_air_spaces.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //((MainActivity)getActivity()).ShowMainMenu(0);
                goBack();

            }
        });
        mSearchEdit = view.findViewById(R.id.searchid);
        mSearchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        view.findViewById(R.id.add).setOnClickListener(this);
        view.findViewById(R.id.importfile).setOnClickListener(this);
        mListView = (ListView) view.findViewById(R.id.listview);
        alarmAdapter = new AlarmAdapter();
        alarmAdapter.setAlarmPoints(AlarmPointManager.getInstance().getPointsLimite(20,0));
        mListView.setAdapter(alarmAdapter);
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
                    View lastVisibleItemView = mListView.getChildAt(mListView.getChildCount() - 1);
                    if (lastVisibleItemView != null && lastVisibleItemView.getBottom() == mListView.getHeight()) {

                       List<AlarmPoint> list =  AlarmPointManager.getInstance().getPointsLimite(20,alarmAdapter.getCount());
                       if(list.size() > 0){
                           alarmAdapter.getAlarmPoints().addAll(list);
                           alarmAdapter.notifyDataSetChanged();
                       }
                    }
                }
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View item, final int position, long id) {
                final View popview = getLayoutInflater().inflate(R.layout.marklist_menu_layout, null);

                final PopupWindow popupWindow = new PopupWindow(popview,
                        200, LinearLayout.LayoutParams.WRAP_CONTENT);

                popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                popupWindow.setFocusable(true);
                popupWindow.setOutsideTouchable(true);
                popupWindow.showAsDropDown(item, item.getWidth() - 20, -(item.getHeight()), Gravity.LEFT | Gravity.TOP);
                popview.findViewById(R.id.modify).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final AlarmInfoDialog dialog = new AlarmInfoDialog(getActivity(),(AlarmPoint) alarmAdapter.getItem(position));
                        dialog.setCallback(new AlarmInfoDialog.IAlarmCallback() {
                            @Override
                            public void onCallback(AlarmPoint point) {
                                alarmAdapter.notifyDataSetChanged();
                                refreshMap();
                                dialog.dismiss();
                            }
                        });
//                        dialog.setiMarkCallback(new ModiMarkDialog.IMarkCallback() {
//                            @Override
//                            public void onSaveMark(Mark mark) {
//                                if (curType > 0)
//                                    marks = MarkManager.getItemsByType(curType);
//                                else {
//                                    marks = MarkManager.getItemsByType(curType);
//                                }
//                                markPointAdapter.notifyDataSetChanged();
//                                updateHideButton();
//                            }
//                        });
                        dialog.show();
                        popupWindow.dismiss();
                    }
                });
                popview.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       AlarmPoint point =  alarmAdapter.getAlarmPoints().get(position);
                        point.delete();
                       alarmAdapter.getAlarmPoints().remove(position);
                       alarmAdapter.notifyDataSetChanged();
                        refreshMap();
                       popupWindow.dismiss();
                    }
                });
                popview.findViewById(R.id.map).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlarmPoint point =  alarmAdapter.getAlarmPoints().get(position);
                        ((MainActivity)getActivity()).getMapControl().slideAnimation(point.getDoubleLongitude(),point.getDoubleLatitude());
                        popupWindow.dismiss();
                    }
                });
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                final AlarmInfoDialog dialog = new AlarmInfoDialog(getActivity());
                dialog.setCallback(new AlarmInfoDialog.IAlarmCallback() {
                    @Override
                    public void onCallback(AlarmPoint point) {
                        alarmAdapter.getAlarmPoints().add(point);
                        alarmAdapter.notifyDataSetChanged();
                        refreshMap();
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
            case R.id.importfile:
                break;
        }

    }

    private class AlarmAdapter extends BaseAdapter {

        List<AlarmPoint> alarmPoints = new ArrayList<>();

        public List<AlarmPoint> getAlarmPoints() {
            return alarmPoints;
        }

        public void setAlarmPoints(List<AlarmPoint> alarmPoints) {
            this.alarmPoints = alarmPoints;
        }

        @Override
        public int getCount() {
            return alarmPoints.size();
        }

        @Override
        public Object getItem(int position) {
            return alarmPoints.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.mark_list_item, null);
                holder = new ViewHolder();
                convertView.setTag(holder);
                holder.img = convertView.findViewById(R.id.markimg);
                holder.name = convertView.findViewById(R.id.name);
                holder.radius = convertView.findViewById(R.id.latlon);
                convertView.findViewById(R.id.details).setVisibility(View.GONE);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            AlarmPoint point = alarmPoints.get(position);
            holder.name.setText(point.getName());
            holder.radius.setText(String.valueOf(point.getRadius()));
            holder.img.setImageBitmap(BrokenLineView.getBitmap(100,100,point.getLineWidth(),point.getLinecolor()));
            return convertView;
        }

        private class ViewHolder {
            ImageView img;
            TextView name;
            TextView radius;
        }
    }
    void refreshMap(){
        LayersFactory.getInstance().updateSingleCustom(AlarmAreaLayer.class, new LayersFactory.IUpdateLister() {
            @Override
            public void onUpdated() {
                ((MainActivity) getActivity()).getMapControl().refresh();
            }
        });
    }
}